﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SignLockerTest
{
    public class SignParser
    {
        public static List<DataPoint> Parser(string content)
        {
            XmlDocument document = new XmlDocument();
            List<DataPoint> result = new List<DataPoint>();
            try
            {
                List<SignPoint> signPoints = new List<SignPoint>();
                document.LoadXml(content);
                XmlElement root = document.DocumentElement;

                XmlNodeList signs = root.GetElementsByTagName("sign");
                foreach (XmlNode signNode in signs)
                {
                    double time = double.Parse(signNode.SelectSingleNode("time").InnerText.Trim());
                    double x = double.Parse(signNode.SelectSingleNode("position").Attributes["x"].Value);
                    double y = double.Parse(signNode.SelectSingleNode("position").Attributes["y"].Value);
                    signPoints.Add(new SignPoint() { Time = time, X = x, Y = y });
                }

                signPoints.Sort((x, y) => x.Time.CompareTo(y.Time));
                List<double> tt = new List<double>();

                for (int i = 1; i < signPoints.Count - 1; i++)
                {
                    CatchedPoint p0 = new CatchedPoint(signPoints[i - 1].X, signPoints[i - 1].Y);
                    CatchedPoint p1 = new CatchedPoint(signPoints[i].X, signPoints[i].Y);
                    CatchedPoint p2 = new CatchedPoint(signPoints[i + 1].X, signPoints[i + 1].Y);
                    double degre = Utility.ComputeDegre(p0, p1, p2);
                    tt.Add(degre);
                    result.Add(new DataPoint() { XValue = signPoints[i].Time, YValue = degre });
                }
            }
            catch (Exception)
            {
                throw;
            }

            return result;
        }
    }

    class SignPoint
    {
        public double Time { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
    }
}
